package com.atguigu.gulimall.search.services.iml;

import com.alibaba.fastjson.JSON;
import com.atguigu.common.no.es.SkuEsModel;
import com.atguigu.gulimall.search.confing.GulimallElasticSearchConfig;
import com.atguigu.gulimall.search.services.ProductSaveService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import javafx.beans.binding.BooleanBinding;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import java.io.IOException;
import java.util.List;

/**
 * @author xiaopeng
 * @date 2020-11-3 20:22
 */
@Service
public class ProductSaveServiceIml implements ProductSaveService {


 @Autowired
 RestHighLevelClient restHighLevelClient;

    @Override
    public void upEsModes(List<SkuEsModel> skuEsModels) {
        //建立缩影
        //给ES中包存数据

        BulkRequest bulkRequest = new BulkRequest();
        for (SkuEsModel model : skuEsModels) {
            IndexRequest indexRequest = new IndexRequest("product");
            indexRequest.id(model.getSkuId().toString());
            String s = JSON.toJSONString(model);
            indexRequest.source(s, XContentType.JSON);
            bulkRequest.add(indexRequest);
            System.out.println("9******************");
        }


        try {
            System.out.println("bulkRequest"+skuEsModels);
            BulkResponse bulkRequest1=  restHighLevelClient.bulk(bulkRequest, GulimallElasticSearchConfig.COMMON_OPTIONS);
           Boolean B= bulkRequest1.hasFailures();
            //日志记录
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
